<template>
    <div class="container">
        <div class="left">
            <img src="../assets/imgs/login_img.png" alt="">
        </div>
        <div class="right">
            <div class="avatar">
                <img src="../assets/imgs/login_logo.png" alt="">
            </div>
            <div class="title">
                电商后台管理系统
            </div>
            <el-form ref="loginFormRef" :model="loginForm" class="login" :rules="loginFormRules">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="el-icon-user"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="el-icon-lock"></el-input>
                </el-form-item>
                <el-button type="primary" class="btn" @click="login">登录</el-button>
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
   data() {
      return {
          loginForm:{
              username:'admin',
              password:'123456'

          },
          loginFormRules:{
              username:[
                  {required: true, message:'请输入用户名', trigger: 'blur'},
                  {require: true, min: 3, max:10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
              ],
              password:[
                  {required: true, message:'请输入密码', trigger: 'blur'},
                  {require: true, min: 5, max:15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
              ]
          }
      }
   },
   created(){
   },
   computed:{
   },
   methods:{
       login(){
           //判断校验是否成功
       this.$refs.loginFormRef.validate(async valid=>{
           if(!valid){
               return
           }
           //发送登录请求
           const{data:res}=await this.$http.post('login',this.loginForm)
        //    判断是否登录成功
        if(res.meta.status!==200){
            return this.$message.error(res.meta.msg)
        }
        this.$message.success('登录成功')
        sessionStorage.setItem('userInfo',JSON.stringify(res.data))
        this.$router.push('/home')
       })
       }
   },
}
</script>
<style lang="less" scoped>
.container{
    width: 100%;
    height: 100%;
    background: #93defe;
    display: flex;
    justify-content: center;
    align-items: center;
    .left{
        margin-right: 100px;
    }
    .right{
        width: 400px;
        height: 350px;
        background: #fff;
        border-radius: 8px;
        position: relative;
        .avatar{
            width: 120px;
            height: 120px;
            border: 5px solid #93defe;
            text-align: center;
            border-radius: 50%;
            margin-top: -60px;
            background: #fff;
            margin-left: 135px;
        }
        .title{
            text-align: center;
            margin-top: 20px;
            font-size: 24px;
            color: #444;
        }
        .login{
            width: 100%;
            padding: 30px 50px;
            box-sizing: border-box;
            // border: 1px solid #000;
            // margin-top: 20px;
            position: absolute;
            left: 50%;
            transform: translate(-50%);
        }
        .btn{
            width: 100%;
        }
    }
}
</style>
